<?php
/**
 * @author : shiminyan
 * @date: 2020/6/3 21:07
 */

namespace app\adminapi\controller;


use app\BaseController;
use app\util\ReturnCode;
use think\App;

class Base extends BaseController
{
    private $debug = [];
    protected $userInfo;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->userInfo=$this->request->API_ADMIN_USER_INFO;
    }

    /**
     * 请求成功
     * 2020/6/4 10:12
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return \think\response\Json
     */
    public function buildSuccess($data=[],$msg='操作成功',$code=ReturnCode::SUCCESS){
        $return = [
            'code' =>$code,
            'msg' =>$msg,
            'data' => $data
        ];
        if (config('app.app_debug') && $this->debug){
            $return['debug']=$this->debug;
        }
        return json($return);
    }

    /**
     * 请求失败
     * 2020/6/4 10:12
     * @param $code
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    public function buildFailed($code,$msg='操作失败',$data=[]){
        $return =[
            'code'=>$code,
            'msg'=>$msg,
            'data'=>$data
        ];
        if (config('app.app_debug') && $this->debug){
            $return['debug']=$this->debug;
        }
        return json($return);
    }

    protected function debug($data){
        if ($data){
            $this->debug[]=$data;
        }
    }


}
